package top.zlhy7.base.response;

import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;

/**
 * @author renyong
 * @date 2023/7/9 18:10
 * @description 统一返回对象
 */
@Data
public class ResultVO<T> {
    /**
     * 响应码
     */
    private int code;
    /**
     * 响应信息
     */
    private String message;
    /**
     * 响应数据
     */
    private T data;
    public ResultVO() {
    }
    public ResultVO(int code, String message) {
        this.code = code;
        this.message = message;
    }
    public static <T> ResultVO<T> ok(){
        return ok(null);
    }
    public static <T> ResultVO<T> ok(T data){
        ResultVO<T> resultVO = new ResultVO<>(HttpStatus.OK.value(), "操作成功");
        resultVO.setData(data);
        return resultVO;
    }
    public static <T> ResultVO<T> error(String msg,T data){
        ResultVO<T> resultVO = new ResultVO<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "操作失败");
        if(!StringUtils.isEmpty(msg)){
            resultVO.setMessage(msg);
        }
        resultVO.setData(data);
        return resultVO;
    }
    public static <T> ResultVO<T> error(String msg){
        return error(msg,null);
    }
}
